// APP
if (window.plus) {
    thirdaccountPageReady();
} else {
    document.addEventListener('plusready', thirdaccountPageReady, false);
}
var thirdaccountPage = function () {
}
// 初始化方法
function thirdaccountPageReady() {
    var _auths = {};
    thirdaccountPage = function () {
        this.init();
    };
    thirdaccountPage.prototype = {
        // 初始化页面
        init: function () {
            if (AGENT_CLIENT.APP) {
                this.getOAuthServices();
            }
        },

        // 获取登录认证通道（APP）
        getOAuthServices: function () {
            plus.oauth.getServices(function (services) {
                for (var i in services) {
                    var service = services[i];
                    // console.log(service.id + ": " + service.authResult + ", " + service.userInfo);
                    _auths[service.id] = service;
                }
            }, function (e) {
                console.log("获取登录认证失败：" + e.message);
            });
        },

        // 第三方登录（APP）
        thirdLogin: function (id) {
            var model = this;
            var auth = _auths[id];
            if (auth) {
                var w = null;
                if (plus.os.name == "Android") {
                    w = plus.nativeUI.showWaiting();
                }
                document.addEventListener("pause", function () {
                    setTimeout(function () {
                        w && w.close();
                        w = null;
                    }, 2000);
                }, false);

                auth.login(function () {
                    w && w.close();
                    w = null;
                    console.log("登录认证成功：");
                    model.getAuthUserInfo(id, auth);
                }, function (e) {
                    w && w.close();
                    w = null;
                    console.log("登录认证失败：");
                    console.log("[" + e.code + "]：" + e.message);
                    //plus.nativeUI.alert("授权登录失败！", null, "登录失败[" + e.code + "]：" + e.message);
                });
            } else {
                console.log("无效的登录认证通道！");
                plus.nativeUI.alert("无效的登录认证通道！", null, "登录");
            }
        },

        // 获取用户信息
        getAuthUserInfo: function (id, auth) {
            var model = this;
            auth.getUserInfo(function () {
                var alist = {"qq": 2, "weixin": 1, "sinaweibo": 3};
                var typeid = alist[id];
                var authuser = null;
                console.log(typeid);
                $.alert( JSON.stringify(auth.userInfo) );
                // 1.微信登录 2.QQ登录 3.新浪登录 4.支付宝登录
                if ([1, 2].indexOf(typeid) > -1) {
                    var nickname = auth.userInfo.nickname || auth.userInfo.name;
                    authuser = {typeid: typeid, openid: auth.authResult.openid, name: nickname};
                } else if (3 == typeid) {
                    authuser = {typeid: typeid, openid: auth.userInfo.id, name: auth.userInfo.name};
                } else {
                    return;
                }
                //console.log(JSON.stringify(authuser));
                model.getUserInfo(authuser);
            }, function (e) {
                console.log("获取用户信息失败：");
                console.log("[" + e.code + "]：" + e.message);
                plus.nativeUI.alert("获取用户信息失败！", null, "登录");
            });
        },

        // 获取用户信息
        getUserInfo: function (authuser) {
        	console.log(JSON.stringify(authuser));
        }

    };
}
